package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.content.Context;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.hzdesk.media.koreabatchmatvector.bean.MouseUserDevicesinfoBean;
import com.hzdesk.media.koreabatchmatvector.config.RxBusUtil;
import com.hzdesk.media.koreabatchmatvector.logic.contract.MouseSetContract;
import com.hzdesk.media.koreabatchmatvector.logic.model.MouseSetModle;
import com.hzdesk.media.koreabatchmatvector.matvector.NiceSpinner;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/10.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class MouseSetPersenter implements MouseSetContract.IMouseSetPersenter {

    private MouseSetContract.IMouseSetView mIMouseSetView ;
    private MouseSetContract.IMouseSetModle mIMouseSetModle;

    private List<String> datalist_1 = new LinkedList<>(Arrays.asList("红", "橙", "黄", "绿", "天蓝", "蓝", "紫", "黑"));                                 //颜色下拉框数据
    private List<String> datalist_3 = new LinkedList<>(Arrays.asList("red_line", "orange_line", "yellow_line", "green_line", "cyan_line", "blue_line", "puple_line", "black_line"));  //注意颜色字母第一个大写
    private List<String> datalist_2 = new LinkedList<>(Arrays.asList("1", "3", "5", "7", "9"));                                                         //笔触下拉框数据
    private List<String> datalist_number = new LinkedList<>(Arrays.asList("一", "二", "三", "四", "五", "六", "七", "八"));                                //鼠标标号数据

    private NiceSpinner tNiceSpinnerOne,tNiceSpinnerTwo;
    private MouseUserDevicesinfoBean touseUserDevicesinfoBean ;

    private int currentPosition,colorPosition,sizePosition;                                                        //当前用户 ,当前颜色 ,当前笔触
    private boolean isChecked = false;
    private Context mContext ;

    public MouseSetPersenter(MouseSetContract.IMouseSetView mIMouseSetView) {
        this.mIMouseSetView = mIMouseSetView;
        mIMouseSetModle = new MouseSetModle();
    }
    @Override
    public void initDate(NiceSpinner mNiceSpinnerOne, NiceSpinner mNiceSpinnerTwo, Context tContext) {
        tNiceSpinnerOne =mNiceSpinnerOne;
        tNiceSpinnerTwo = mNiceSpinnerTwo;
        mContext = tContext;
        tNiceSpinnerOne.attachDataSource(datalist_1);
        tNiceSpinnerTwo.attachDataSource(datalist_2);
        tNiceSpinnerOne.setOnItemSelectedListener(itemSelectedListener_1);
        tNiceSpinnerTwo.setOnItemSelectedListener(itemSelectedListener_2);
    }

    @Override
    public void setinitListener(CheckBox mCheckBox, Context mContext,int postion) {
        updateView(postion,mCheckBox);
    }

    @Override
    public void setOnGenericMotionListener(NiceSpinner mNiceSpinnerOne, NiceSpinner mNiceSpinnerTwo) {
        mNiceSpinnerOne.setOnGenericMotionListener(onGenericMotionListener_1);
        mNiceSpinnerTwo.setOnGenericMotionListener(onGenericMotionListener_2);
    }

    @Override
    public void onClickCancle() {
        mIMouseSetView.toFinshPager();
    }

    @Override
    public void onClickConfirm(Context mContext) {
        try {
            String name = mIMouseSetView.getMouseSetName();
            int DisMouse = isChecked? 1:0;
            String CursorColor  =   datalist_3.get(colorPosition);
            int BrushSize = Integer.parseInt(datalist_2.get(sizePosition));
            //将对应鼠标用户的信息更新修改数据库
            if(touseUserDevicesinfoBean != null){
                touseUserDevicesinfoBean.setName(name);
                touseUserDevicesinfoBean.setDisMouse(DisMouse);
                touseUserDevicesinfoBean.setCursorColor(CursorColor);
                touseUserDevicesinfoBean.setBrushSize(BrushSize);
                // 更新数据库
                mIMouseSetModle.update(touseUserDevicesinfoBean,mContext);
            }
        } catch (NumberFormatException e) {}
        mIMouseSetView.toFinshPager();
    }


    //  根据几号笔触 显示对应的信息 几号鼠标 笔触颜色索引 是否点击 选中事件 取消 确定
    private void updateView(int position, CheckBox mCheckBox) {
        currentPosition = position;
        // 从数据库获取 对应的鼠标数据展示
        List<MouseUserDevicesinfoBean>  datelist =  mIMouseSetModle.query(mContext);
        // 遍历集合
        if(datelist != null && datelist.size() >0){
            // 查询对应的鼠标展示鼠标各种信息
            for (MouseUserDevicesinfoBean mouseUserDevicesinfoBean : datelist) {
                if(mouseUserDevicesinfoBean.getMouseNum() == (position+1 )){
                    touseUserDevicesinfoBean = mouseUserDevicesinfoBean;
                    mIMouseSetView.setTvNumber(datalist_number.get(position) + "号鼠标");
                    mCheckBox.setOnCheckedChangeListener(onCheckedChangeListener); //mouseUserDevicesinfoBean.getDisMouse() ==0?false:true
                    mCheckBox.setChecked(mouseUserDevicesinfoBean.getDisMouse() ==0?false:true);
                    //展示用户名称  光标样式  笔触大小
                    mIMouseSetView.setupdateView(mouseUserDevicesinfoBean);
                    tNiceSpinnerOne.setSelectedIndex(datalist_3.indexOf(mouseUserDevicesinfoBean.getCursorColor()));
                    tNiceSpinnerTwo.setSelectedIndex(datalist_2.indexOf(mouseUserDevicesinfoBean.getBrushSize()+""));
                }
            }
        }else{
            mIMouseSetView.setMouseSetName(datalist_number.get(currentPosition)+"号用户");
            tNiceSpinnerOne.setSelectedIndex(2);
            tNiceSpinnerTwo.setSelectedIndex(2);
        }
    }



    private AdapterView.OnItemSelectedListener itemSelectedListener_1 = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            colorPosition = position;
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {}
    };
    private  AdapterView.OnItemSelectedListener itemSelectedListener_2 = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            sizePosition = position;
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {}
    };
    // 鼠标滚动事件
    private View.OnGenericMotionListener onGenericMotionListener_2 = new View.OnGenericMotionListener() {
        @Override
        public boolean onGenericMotion(View v, MotionEvent event) {
            if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_SCROLL:
                        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f) {
                            if (sizePosition < 4) {
                                sizePosition++;
                            }
                        } else {
                            if (sizePosition > 0) {
                                sizePosition--;
                            }
                        }
                        mIMouseSetView.setTwoSelectedIndex(sizePosition);
                        break;
                }
            }
            return false;
        }
    };
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
            isChecked = ischecked;
        }
    };
    private View.OnGenericMotionListener onGenericMotionListener_1 = new View.OnGenericMotionListener() {
        @Override
        public boolean onGenericMotion(View v, MotionEvent event) {
            if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_SCROLL:
                        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f) {
                            if (colorPosition < 7) {
                                colorPosition++;
                            }
                        } else {
                            if (colorPosition > 0) {
                                colorPosition--;
                            }
                        }
                        mIMouseSetView.setOneSelectedIndex(colorPosition);
                        break;
                }
            }
            return false;
        }
    };
}
